package com.zt.teacher.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.Teachers;
import com.zt.common.pojo.netty.Sender;
import com.zt.common.utils.JwtUtil;
import com.zt.common.utils.RedisUtil;
import com.zt.teacher.dao.TeacherDao;
import com.zt.teacher.service.TeacherService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * description:
 * author:zt
 * date:2021-12-07
 */

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    private static final Log log = LogFactory.get();

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Sender login(Teachers teacher, HttpServletResponse response) {

        String teacherId = teacher.getTeacherId();
        String password = teacher.getPassword();

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(teacherId, password);

        try {
            log.info(teacher + "正在登录");
            subject.login(token);
        }catch (UnknownAccountException ue){
            throw new ApiException(CodeMsg.USER_NOT_FOUND);
        }catch (IncorrectCredentialsException ie){
            throw new ApiException(CodeMsg.WRONG_PASSWORD);
        }

        Teachers teacherDB = findTeacherById(teacherId);
        teacherDB.setLastLogin(new Date());

        teacherDao.update(teacherDB);
        log.info(teacherDB + "登陆成功");

        String reToken = JwtUtil.getToken(teacherId, String.valueOf(System.currentTimeMillis()),teacherDB.getType());
        response.setHeader("authorization", reToken);
        response.setHeader("access-Control-Expose-Headers", "Authorization");

        return new Sender(teacherId,teacherDB.getName(),teacherDB.getAvatar());
    }

    @Override
    public Teachers findTeacherById(String teacherId) {
        return teacherDao.findTeacherById(teacherId);
    }

    @Override
    public void logOut(HttpServletRequest request){
        String teacherId = JwtUtil.decodedGetID(request.getHeader("Authorization"));
        if (redisUtil.hasKey(teacherId))
            redisUtil.del(teacherId);
        log.info(teacherId + "退出登录");
    }

    @Override
    public boolean getIdentity(HttpServletRequest request){
        String identity = JwtUtil.getIdentity(request.getHeader("Authorization"));
        return identity.equals("2");
    }
}
